package cn.ittaoli.ajax.service.impl;

import cn.ittaoli.ajax.dao.ArticleCommentMapper;
import cn.ittaoli.ajax.dao.ArticleContentMapper;
import cn.ittaoli.ajax.dao.ArticleMapper;
import cn.ittaoli.ajax.model.*;
import cn.ittaoli.ajax.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lub_lenovo on 2017/8/9.
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleCommentMapper articleCommentMapper;

    public int insertArticle(ArticleFull articleFull) {
        articleFull.setInserttime(System.currentTimeMillis());
//        返回值是影响的行数，不是新增数据的id
        int res=articleMapper.insertSelective((Article) articleFull);

        ArticleContent articleContent = new ArticleContent();
        articleContent.setArticleid(articleFull.getId());
        articleContent.setContent(articleFull.getContent());
        articleContentMapper.insertSelective(articleContent);

        return articleFull.getId();
    }

    public List<ArticleView> getAll() {
        return articleMapper.getAll();
    }

    public List<ArticleCommentTree> getAllComment(Integer articleId,Integer replyId) {
        return articleMapper.getAllComment(articleId,replyId);
    }

    public PageInfo<ArticleView>  getByPage(Integer page, Integer rowsPerPage) {
        PageHelper.startPage(page,rowsPerPage);
        List<ArticleView> articleViews=articleMapper.getAll();
        PageInfo<ArticleView> pageInfo=new PageInfo<ArticleView>(articleViews);
        return pageInfo;
    }

    public PageInfo<ArticleView> getByPagePhone(Integer page, Integer rowsPerPage, Integer navid) {
        PageHelper.startPage(page,rowsPerPage);
        if(navid>0){
            List<ArticleView> articleViews=articleMapper.getAllPhone(navid);
            PageInfo<ArticleView> pageInfo=new PageInfo<ArticleView>(articleViews);
            return pageInfo;
        }else{
            List<ArticleView> articleViews=articleMapper.getAll();
            PageInfo<ArticleView> pageInfo=new PageInfo<ArticleView>(articleViews);
            return pageInfo;
        }
    }

    public Map<String, Object> getArticle(Integer id) {
        Article article=articleMapper.selectByPrimaryKey(id);
        ArticleContent articleContent=articleContentMapper.selectByArticleId(id);
        List<ArticleComment> articleComments=articleCommentMapper.selectByArticleId(id);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("article",article);
        map.put("ac",articleContent);
        map.put("act",articleComments);
        return map;
    }
}
